package com.xique.park.controller.web;

import cn.hutool.core.date.DateUtil;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.request.comm.EditParkOnRequest;
import com.xique.park.bean.request.comm.MarkOutParkOnRequest;
import com.xique.park.bean.request.comm.ParkOnEditPlateSaveRequest;
import com.xique.park.bean.request.comm.ParkOnListRequest;
import com.xique.park.bean.request.web.parkOn.BatchMarkOutParkOnRequest;
import com.xique.park.service.biz.service.IParkOnBizService;
import com.xique.park.service.service.ExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 在场记录controller
 *
 * @author xique
 */
@Slf4j
@RestController
@RequestMapping("/web/parkOn")
public class WebParkOnController {

    @Autowired
    private IParkOnBizService parkOnBizService;

    @Autowired
    private ExportService exportService;

    /**
     * 查询在场记录列表
     */
    @PostMapping("/list")
    @PreAuthorize(hasPermi = "park:parkOn:list")
    public AjaxResult parkOnList(@Validated @RequestBody ParkOnListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return parkOnBizService.getParkOnLst(request);
    }

    /**
     * 修改车牌信息回显
     */
    @PostMapping("/getPlateInfo")
    @PreAuthorize(hasPermi = "park:parkOn:query")
    public AjaxResult getParkOnEditPlateInfo(@Validated @RequestBody EditParkOnRequest request) {
        return parkOnBizService.getParkOnEditPlateInfo(request);
    }

    /**
     * 车辆详情
     */
    @PostMapping("/detail")
    @PreAuthorize(hasPermi = "park:parkOn:query")
    public AjaxResult detail(@Validated @RequestBody EditParkOnRequest request) {
        return parkOnBizService.detail(request);
    }

    /**
     * 修改车牌信息保存
     */
    @PostMapping("/editPlateInfo")
    @Log(title = "在场车牌", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "park:parkOn:edit")
    public AjaxResult editPlateInfo(@Validated @RequestBody ParkOnEditPlateSaveRequest request) {
        return parkOnBizService.editPlateInfo(request);
    }

    /**
     * 标记出场
     */
    @PostMapping("/markOut")
    @Log(title = "在场记录-标记出场", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermi = "park:parkOn:remove")
    public AjaxResult markOut(@Validated @RequestBody MarkOutParkOnRequest request) {
        request.setStatus("250");
        request.setOutRemark("人工标记出场");
        return parkOnBizService.markOut(request);
    }

    /**
     * 批量标记出场
     */
    @PostMapping("/batchMarkOut")
    @Log(title = "在场记录-批量标记出场", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermi = "park:parkOn:remove")
    public AjaxResult batchMarkOut(@Validated @RequestBody BatchMarkOutParkOnRequest request) {
        return parkOnBizService.batchMarkOut(request);
    }

    /**
     * 查询剩余车位信息
     */
    @GetMapping("/getSpacesInfo")
    @PreAuthorize(hasPermi = "park:parkOn:list")
    public AjaxResult getSpacesInfo() {
        return parkOnBizService.getSpacesInfo();
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ParkOn> util = new ExcelUtil<>(ParkOn.class);
        util.importTemplateExcel(response, "在场记录");
    }

    /**
     * 导入数据（批量进场）
     * 增加车场id筛选
     */
    @PostMapping("/importData")
    @PreAuthorize(hasPermi = "park:parkOn:import")
    @Log(title = "在场记录", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ParkOn> util = new ExcelUtil<>(ParkOn.class);
        List<ParkOn> parkOnList = util.importExcel(file.getInputStream());
        return parkOnBizService.importData(SecurityUtils.getParkId(), parkOnList, updateSupport);
    }

    /**
     * 导出
     */
    @Log(title = "在场记录", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "park:parkOn:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody ParkOnListRequest request) throws IOException {
        request.setParkId(SecurityUtils.getParkId());
        exportService.exportParkOnList(response,request);
    }

    /**
     * 定时删除在场记录
     */
    @PostMapping("/timingDelete")
    public R<String> timingDelete() {
        log.info("[在场记录]-定时删除在场记录，开始时间：{}", DateUtil.now());
        parkOnBizService.timingDelete();
        log.info("[在场记录]-定时删除在场记录，结束时间：{}", DateUtil.now());
        return R.ok();
    }
}
